package com.oliet.util;

import java.io.File;

/**
 * @author jiaZhenpeng
 * @Title: FileUtils
 * @Package com.oliet.util
 * @Description: 文件工具类
 * @date 2023-12-24 11:05
 */
public class MyFileUtils {

    /**
     * 创建保存路径
     *
     * @param prefixPath
     * @param fileName
     * @return
     */
    public static String createSaveFolder(String prefixPath, String fileName) {
        String saveType = getFileSaveType(fileName);
        String saveFolder = prefixPath + File.separator + saveType;
        if (!new File(saveFolder).exists()) {
            new File(saveFolder).mkdirs();
        }
        return saveFolder;
    }

    /**
     * 获取保存文件夹类型
     *
     * @param fileName
     * @return
     */
    public static String getFileSaveType(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return "other";
        }
        if (!hasSuffix(fileName)) {
            return "other";
        }
        String extension = getSuffix(fileName);
        switch (extension) {
            case "jpg":
            case "jpeg":
            case "png":
            case "gif":
                return "picture";
            case "pdf":
            case "doc":
            case "docx":
            case "xls":
            case "xlsx":
                return "document";
            case "mp4":
            case "avi":
            case "mkv":
                return "video";
            case "mp3":
            case "wav":
                return "music";
            case "zip":
                return "unzip";
            default:
                return "other";
        }
    }

    public static String getSuffix(String fileName){
        if (!hasSuffix(fileName)){
            return "";
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
    }

    /**
     * 判断文件名是否有后缀
     *
     * @param fileName
     * @return
     */
    public static boolean hasSuffix(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return false;
        }
        int lastIndexOfDot = fileName.lastIndexOf('.');
        return lastIndexOfDot != -1 && lastIndexOfDot != fileName.length() - 1;
    }


}
